package views.main
{
	import commands.supplier.FindAssociationsCmd;
	import commands.supplier.FindProductCategory;
	
	import components.product.AssociationGroup;
	import components.product.ProductSprite;
	
	import cultu.supplier.message.Association;
	import cultu.supplier.message.Product;
	
	import model.Model;
	
	import spark.layouts.TileLayout;
	import spark.layouts.VerticalLayout;
	
	import utils.CultuEvent;
	
	import vo.ProductVO;
	
	import wizeoo.commands.WIZGroupCommand;
	import wizeoo.events.WIZCommandEvent;
	import wizeoo.interfaces.WIZPilotAncestor;
	import wizeoo.masters.MasterPilot;
	import wizeoo.models.WIZGlobalParameters;

	public class MainViewPilot extends WIZPilotAncestor
	{
		public function MainViewPilot()
		{
			super();
		}
		
		[Bindable]  
		override public var masterPilot: MasterPilot;
		
		public var view:MainView;
		
		/**Référence au model*/
		private var mod:Model = Model.getInstance();
		
		override public function get initCommand():WIZGroupCommand
		{
			var ar:Array = [];
			
			ar.push(new FindAssociationsCmd());
			
			if(ar.length > 0)
				return new WIZGroupCommand(ar,true);
			
			return null;
		}
		
		override public function wizCreation():void
		{
			view=masterPilot.linkedView as MainView;
		}
		
		override public function wizSynchronizeData():void
		{
			if(mod.currentAssociations)
			{
				drawPage();
			}
		}
		
		private const CHILDREN_WIDTH:int = 140;
		
		private const CHILDREN_HEIGHT:int = 220;
		
		/**dessine la page en fonction des associations*/
		private function drawPage():void
		{
			view.removeAllElements();
			
			/*for(var i:int=0;i<mod.currentAssociations.length;i++)
			{
				var assoc:Association = mod.currentAssociations.getItemAt(i) as Association;
				
				var pan:WIZPanel = new WIZPanel();
				pan.percentWidth = 100;
				pan.title = assoc.community.universe;
				
				var list:WIZList = new WIZList;
				var coll:ArrayCollection = new ArrayCollection();
				for(var j:int=0;j<assoc.products.length;j++)
				{
					coll.addItem(new ProductVO(assoc.products.getItemAt(j) as Product));
				}
				list.dataProvider = coll;
				list.height = 210;
				list.percentWidth = 100;
				var layout:HorizontalLayout = new HorizontalLayout();
				layout.gap = 10;
				list.layout = layout;
				list.styleName = 'invisibleList';
				list.itemRenderer = new ClassFactory(ProductRenderer);
				
				pan.addElement(list);
				
				view.addElement(pan);
			}*/
			//view.listAssoc.dataProvider = mod.currentAssociations;
			
			if(!mod.selectedCategory)
			{
				var layout:VerticalLayout = new VerticalLayout();
				layout.gap = 10;
				view.layout = layout;
				
				for(var i:int=0;i<mod.currentAssociations.length;i++)
				{
					var assoc:Association = mod.currentAssociations.getItemAt(i) as Association;
					
					if(assoc.products.length > 0)
					{
						var assocGroup:AssociationGroup = new AssociationGroup();
						assocGroup.width = view.width;
						assocGroup.height = 260;
						assocGroup.data = assoc;
						view.addElement(assocGroup);
					}
				}
			}
			else
			{
				var layout2:TileLayout = new TileLayout();
				layout2.verticalGap = 10;
				layout2.horizontalGap = 10;
				view.layout = layout2;
				
				for(var j:int = 0;j<mod.productsCategoryColl.length;j++)
				{
					var p:ProductVO = mod.productsCategoryColl.getItemAt(j) as ProductVO;
					
					var productSprite:ProductSprite = new ProductSprite();
					
					view.addElement(productSprite);
					
					productSprite.width = CHILDREN_WIDTH;
					productSprite.height = CHILDREN_HEIGHT;
					productSprite.data = p;
				}
			}
			
		}
		
		private function onLoaded(e:WIZCommandEvent):void
		{
			if(e.type == WIZCommandEvent.COMMAND_SUCCES)
			{
				wizSynchronizeData();
			}
		}
		
		private function onCategoryChange(pEvent:CultuEvent):void
		{
			if(mod.selectedCategory)
				new FindProductCategory(onLoaded).execute();
			else
				new FindAssociationsCmd(onLoaded).execute();
		}
		
		private function onRefresh(pEvent:CultuEvent):void
		{
			new FindAssociationsCmd(onLoaded).execute();
		}
		
		override public function wizAddListeners():void
		{
			WIZGlobalParameters.root.addEventListener(CultuEvent.REFRESH_MAIN_VIEW,onRefresh,false,0,true);
			WIZGlobalParameters.root.addEventListener(CultuEvent.CATEGORY_CHANGE,onCategoryChange,false,0,true);
		}
		
		override public function wizRemoveListeners():void
		{
			WIZGlobalParameters.root.removeEventListener(CultuEvent.REFRESH_MAIN_VIEW,onRefresh);
			WIZGlobalParameters.root.removeEventListener(CultuEvent.CATEGORY_CHANGE,onCategoryChange);
		}
		
		override public function wizRefresh():void
		{
			
		}
		
		override public function wizExit():void
		{
			
		}
		
	}
}